package com.otdemo.service;

import com.otdemo.dao.RestClassificationDao;
import com.otdemo.dao.RestContentDao;
import com.otdemo.dao.RestScoreDao;
import com.otdemo.entity.RestClassification;
import com.otdemo.entity.RestContent;
import com.otdemo.entity.RestScore;
import com.otdemo.resp.RestContentResp;
import com.otdemo.resp.ResultResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class RestContentService {

    public List<RestContentResp> initList() {
        List<RestContent> list = new RestContentDao().list();
        List<RestScore> scoreList = new RestScoreDao().list();
        List<RestClassification> classificationList = new RestClassificationDao().list();
        List<RestContentResp> respList = new ArrayList<>();
        try {
            int index = 1;
            for (RestContent rest : list) {
                for (RestClassification restClassification : classificationList) {
                    RestContentResp resp = new RestContentResp();
                    resp.setName(rest.getName());
                    resp.setId(index);
                    index++;
                    resp.setClassificationName(restClassification.getName());
                    List<RestScore> scoreList1 = scoreList.stream().filter(p -> p.getContent().equals(rest.getName())
                            && p.getClassification().equals(restClassification.getName())).collect(Collectors.toList());
                    BigDecimal score = new BigDecimal(0.00);
                    for (RestScore restScore : scoreList1) {
                        score = score.add(restScore.getScore());
                    }
                    if (scoreList1.size() == 0) {
                        resp.setScore(new BigDecimal(0.00));
                    } else {
                        resp.setScore(score.divide(new BigDecimal(scoreList1.size()), 2, BigDecimal.ROUND_UP));
                    }
                    respList.add(resp);
                }
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return respList;
    }

    /**
     * 新增
     *
     * @return
     */
    public ResultResponse insert(RestContent rest) {
        ResultResponse resultResponse = new ResultResponse();
        RestContent rest1 = new RestContentDao().findByUid(rest.getName());
        if (rest1 != null) {
            return resultResponse.fail("名称重复");
        }
        try {
            new RestContentDao().insert(rest);
        } catch (Exception e) {
            return resultResponse.fail(e.getMessage());
        }
        return resultResponse.success();
    }

    /**
     * 更新
     *
     * @return
     */
    public ResultResponse update(RestContent rest) {
        ResultResponse resultResponse = new ResultResponse();
        RestContent rest1 = new RestContentDao().findByUid(rest.getUid());
        if (rest1 == null) {
            return resultResponse.fail("查询不存在");
        }
        try {
            new RestContentDao().update(rest);
        } catch (Exception e) {
            return resultResponse.fail(e.getMessage());
        }
        return resultResponse.success();
    }

//    public ResultResponse delete(Content rest) {
//        ResultResponse resultResponse = new ResultResponse();
//        Content rest1 = new RestContentDao().findByUid(rest.getName());
//        if(rest1 == null){
//            return resultResponse.fail("图书不存在");
//        }
//        try {
//            new RestContentDao().delete(rest.getUid());
//        } catch (Exception e) {
//            return resultResponse.fail(e.getMessage());
//        }
//        return resultResponse.success();
//    }

    /**
     * 查询
     *
     * @return
     */
    public ResultResponse findByUid(RestContent rest) {
        ResultResponse resultResponse = new ResultResponse();
        try {
            RestContent rest1 = new RestContentDao().findByUid(rest.getUid());
            return resultResponse.success(rest1);
        } catch (Exception e) {
            return resultResponse.fail(e.getMessage());
        }
    }

    public String[] contentList() {
        try {
            List<RestContent> list = new RestContentDao().list();
            List<String> names = list.stream().map(p -> p.getName()).collect(Collectors.toList());
            String[] strArray = new String[names.size() + 1];
            return names.toArray(strArray);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
